package com.football.springboot.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.football.springboot.common.KeepTwoDecimal;
import com.football.springboot.entity.*;
import com.football.springboot.mapper.PlayerDataMapper;
import com.football.springboot.service.IGameService;
import com.football.springboot.service.IPlayerDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李越
 * @since 2023-03-03
 */
@Service
public class PlayerDataServiceImpl extends ServiceImpl<PlayerDataMapper, PlayerData> implements IPlayerDataService {

    @Resource
    private PlayerDataMapper playerDataMapper;

    @Resource
    private IGameService gameService;



    @Override
    public PlayerData addplayerdata(PlayerDataSingle playerDataSingle) {
        //新增球员比赛数据时添加该球员的赛季数据（出场次数从0到1）
        PlayerData playerData = new PlayerData();
        //设置球员id 球队id 联赛id 联赛赛季
        playerData.setPlayerId(playerDataSingle.getPlayerId());
        playerData.setTeamId(playerDataSingle.getTeamId());
        playerData.setLeagueId(gameService.getById(playerDataSingle.getGameId()).getLeagueId());
        playerData.setLeagueSeason(gameService.getLeagueinfo(playerDataSingle.getGameId()).getLeagueSeason());
        //设置出场次数
        playerData.setGamesPlayed(1);

        //设置其他参数
        playerData.setTotalGoal(playerDataSingle.getSingleGoal());
        playerData.setTotalAssist(playerDataSingle.getSingleAssist());
        playerData.setTotalShoot(playerDataSingle.getSingleShoot());
        playerData.setTotalOntarget(playerDataSingle.getSingleOntarget());
        playerData.setTotalPass(playerDataSingle.getSinglePass());
        playerData.setTotalOkpass(playerDataSingle.getSingleOkpass());
        playerData.setTotalSurpass(playerDataSingle.getSingleSurpass());
        playerData.setTotalOksurpass(playerDataSingle.getSingleOksurpass());
        playerData.setTotalPreemption(playerDataSingle.getSinglePreemption());
        playerData.setTotalOffside(playerDataSingle.getSingleOffside());
        playerData.setTotalFoul(playerDataSingle.getSingleFoul());
        playerData.setTotalBefouled(playerDataSingle.getSingleBefouled());
        playerData.setTotalYcard(playerDataSingle.getSingleYcard());
        playerData.setTotalRcard(playerDataSingle.getSingleRcard());

        return playerData;
    }

    @Override
    public PlayerData getupdateplayerdata(Integer playerId,  Integer leagueId) {
        return playerDataMapper.getupdateplayerdata(playerId,leagueId);
    }

    @Override
    public PlayerData updateplayerdata(PlayerData playerData, PlayerDataSingle playerDataSingle) {
        //新增球员的赛季比赛数据(比赛数从1到n，新增出场次数2及以上的球员比赛数据时)
        //设置出场次数
        playerData.setGamesPlayed(playerData.getGamesPlayed()+1);
        //设置其余参数（球员id 球队id 联赛id 联赛赛季都不用设置）
        playerData.setTotalGoal(playerData.getTotalGoal()+playerDataSingle.getSingleGoal());
        playerData.setTotalAssist(playerData.getTotalAssist()+playerDataSingle.getSingleAssist());
        playerData.setTotalShoot(playerData.getTotalShoot()+playerDataSingle.getSingleShoot());
        playerData.setTotalOntarget(playerData.getTotalOntarget()+playerDataSingle.getSingleOntarget());
        playerData.setTotalPass(playerData.getTotalPass()+playerDataSingle.getSinglePass());
        playerData.setTotalOkpass(playerData.getTotalOkpass()+playerDataSingle.getSingleOkpass());
        playerData.setTotalSurpass(playerData.getTotalSurpass()+playerDataSingle.getSingleSurpass());
        playerData.setTotalOksurpass(playerData.getTotalOksurpass()+playerDataSingle.getSingleOksurpass());
        playerData.setTotalPreemption(playerData.getTotalPreemption()+playerDataSingle.getSinglePreemption());
        playerData.setTotalOffside(playerData.getTotalOffside()+playerDataSingle.getSingleOffside());
        playerData.setTotalFoul(playerData.getTotalFoul()+playerDataSingle.getSingleFoul());
        playerData.setTotalBefouled(playerData.getTotalBefouled()+playerDataSingle.getSingleBefouled());
        playerData.setTotalYcard(playerData.getTotalYcard()+playerDataSingle.getSingleYcard());
        playerData.setTotalRcard(playerData.getTotalRcard()+playerDataSingle.getSingleRcard());


        return playerData;
    }



    @Override
    public PlayerData updateplayerdataexist(PlayerData playerData, PlayerDataSingle playerDataSinglebefore, PlayerDataSingle playerDataSingle) {
        //修改球员的赛季比赛数据（当你修改已经添加的球员比赛数据时）

        //未修改之前的球员单场比赛数据

        Double singlegoalbefore = playerDataSinglebefore.getSingleGoal();
        Double singleassistbefore = playerDataSinglebefore.getSingleAssist();
        Double singleshootbefore = playerDataSinglebefore.getSingleShoot();
        Double singleontargetbefore = playerDataSinglebefore.getSingleOntarget();
        Double singlepassbefore = playerDataSinglebefore.getSinglePass();
        Double singleokpassbefore = playerDataSinglebefore.getSingleOkpass();
        Double singlesurpassbefore = playerDataSinglebefore.getSingleSurpass();
        Double singleoksurpassbefore = playerDataSinglebefore.getSingleOksurpass();
        Double singlepreemptionbefore = playerDataSinglebefore.getSinglePreemption();
        Double singleoffsidebefore = playerDataSinglebefore.getSingleOffside();
        Double singlefoulbefore = playerDataSinglebefore.getSingleFoul();
        Double singlebefouledbefore = playerDataSinglebefore.getSingleBefouled();
        Double singleycardbefore = playerDataSinglebefore.getSingleYcard();
        Double singlercardbefore = playerDataSinglebefore.getSingleRcard();

        //修改之后的球员单场比赛数据
        Double singlegoal = playerDataSingle.getSingleGoal();
        Double singleassist = playerDataSingle.getSingleAssist();
        Double singleshoot = playerDataSingle.getSingleShoot();
        Double singleontarget = playerDataSingle.getSingleOntarget();
        Double singlepass = playerDataSingle.getSinglePass();
        Double singleokpass = playerDataSingle.getSingleOkpass();
        Double singlesurpass = playerDataSingle.getSingleSurpass();
        Double singleoksurpass = playerDataSingle.getSingleOksurpass();
        Double singlepreemption = playerDataSingle.getSinglePreemption();
        Double singleoffside = playerDataSingle.getSingleOffside();
        Double singlefoul = playerDataSingle.getSingleFoul();
        Double singlebefouled = playerDataSingle.getSingleBefouled();
        Double singleycard = playerDataSingle.getSingleYcard();
        Double singlercard = playerDataSingle.getSingleRcard();

        playerData.setTotalGoal(playerData.getTotalGoal()-singlegoalbefore+singlegoal);
        playerData.setTotalAssist(playerData.getTotalAssist()-singleassistbefore+singleassist);
        playerData.setTotalShoot(playerData.getTotalShoot()-singleshootbefore+singleshoot);
        playerData.setTotalOntarget(playerData.getTotalOntarget()-singleontargetbefore+singleontarget);
        playerData.setTotalPass(playerData.getTotalPass()-singlepassbefore+singlepass);
        playerData.setTotalOkpass(playerData.getTotalOkpass()-singleokpassbefore+singleokpass);
        playerData.setTotalSurpass(playerData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
        playerData.setTotalOksurpass(playerData.getTotalOksurpass()-singleoksurpassbefore+singleoksurpass);
        playerData.setTotalPreemption(playerData.getTotalPreemption()-singlepreemptionbefore+singlepreemption);
        playerData.setTotalOffside(playerData.getTotalOffside()-singleoffsidebefore+singleoffside);
        playerData.setTotalFoul(playerData.getTotalFoul()-singlefoulbefore+singlefoul);
        playerData.setTotalBefouled(playerData.getTotalBefouled()-singlebefouledbefore+singlebefouled);
        playerData.setTotalYcard(playerData.getTotalYcard()-singleycardbefore+singleycard);
        playerData.setTotalRcard(playerData.getTotalRcard()-singlercardbefore+singlercard);


        return playerData;
    }

    @Override
    public Team getteaminfo(Integer teamId) {
        return playerDataMapper.getteaminfo(teamId);
    }

    @Override
    public League getleagueinfo(Integer leagueId) {
        return playerDataMapper.getleagueinfo(leagueId);
    }

    @Override
    public Player getplayerinfo(Integer playerId) {
        return playerDataMapper.getplayerinfo(playerId);
    }

    @Override
    public void countplayerleague(PlayerData playerData) {
        KeepTwoDecimal keepTwoDecimal=new KeepTwoDecimal();
        playerData.setAverageGoal(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalGoal()/playerData.getGamesPlayed()));
        playerData.setAverageAssist(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalAssist()/playerData.getGamesPlayed()));
        playerData.setAverageShoot(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalShoot()/playerData.getGamesPlayed()));
        playerData.setAverageOntarget(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalOntarget()/playerData.getGamesPlayed()));
        playerData.setAveragePass(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalPass()/playerData.getGamesPlayed()));
        playerData.setAverageOkpass(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalOkpass()/playerData.getGamesPlayed()));
        playerData.setAverageSurpass(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalSurpass()/playerData.getGamesPlayed()));
        playerData.setAverageOksurpass(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalOksurpass()/playerData.getGamesPlayed()));
        playerData.setAveragePreemption(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalPreemption()/playerData.getGamesPlayed()));
        playerData.setAverageOffside(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalOffside()/playerData.getGamesPlayed()));
        playerData.setAverageFoul(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalFoul()/playerData.getGamesPlayed()));
        playerData.setAverageBefouled(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalBefouled()/playerData.getGamesPlayed()));
        playerData.setAverageYcard(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalYcard()/playerData.getGamesPlayed()));
        playerData.setAverageRcard(keepTwoDecimal.KeepTwoDecimal(playerData.getTotalRcard()/playerData.getGamesPlayed()));
    }

    @Override
    public PlayerData playerDataallleague(List<PlayerData> playerDataList, PlayerData playerDataall) {
        playerDataall.setGamesPlayed(0);
        playerDataall.setTotalGoal(0.0);
        playerDataall.setTotalAssist(0.0);
        playerDataall.setTotalShoot(0.0);
        playerDataall.setTotalOntarget(0.0);
        playerDataall.setTotalPass(0.0);
        playerDataall.setTotalOkpass(0.0);
        playerDataall.setTotalSurpass(0.0);
        playerDataall.setTotalOksurpass(0.0);
        playerDataall.setTotalPreemption(0.0);
        playerDataall.setTotalOffside(0.0);
        playerDataall.setTotalFoul(0.0);
        playerDataall.setTotalBefouled(0.0);
        playerDataall.setTotalYcard(0.0);
        playerDataall.setTotalRcard(0.0);
        for (int i=0;i<playerDataList.size();i++){
            playerDataall.setGamesPlayed(playerDataall.getGamesPlayed()+playerDataList.get(i).getGamesPlayed());
            playerDataall.setTotalGoal(playerDataall.getTotalGoal()+playerDataList.get(i).getTotalGoal());
            playerDataall.setTotalAssist(playerDataall.getTotalAssist()+playerDataList.get(i).getTotalAssist());
            playerDataall.setTotalShoot(playerDataall.getTotalShoot()+playerDataList.get(i).getTotalShoot());
            playerDataall.setTotalOntarget(playerDataall.getTotalOntarget()+playerDataList.get(i).getTotalOntarget());
            playerDataall.setTotalPass(playerDataall.getTotalPass()+playerDataList.get(i).getTotalPass());
            playerDataall.setTotalOkpass(playerDataall.getTotalOkpass()+playerDataList.get(i).getTotalOkpass());
            playerDataall.setTotalSurpass(playerDataall.getTotalSurpass()+playerDataList.get(i).getTotalSurpass());
            playerDataall.setTotalOksurpass(playerDataall.getTotalOksurpass()+playerDataList.get(i).getTotalOksurpass());
            playerDataall.setTotalPreemption(playerDataall.getTotalPreemption()+playerDataList.get(i).getTotalPreemption());
            playerDataall.setTotalOffside(playerDataall.getTotalOffside()+playerDataList.get(i).getTotalOffside());
            playerDataall.setTotalFoul(playerDataall.getTotalFoul()+playerDataList.get(i).getTotalFoul());
            playerDataall.setTotalBefouled(playerDataall.getTotalBefouled()+playerDataList.get(i).getTotalBefouled());
            playerDataall.setTotalYcard(playerDataall.getTotalYcard()+playerDataList.get(i).getTotalYcard());
            playerDataall.setTotalRcard(playerDataall.getTotalRcard()+playerDataList.get(i).getTotalRcard());
        }

        return playerDataall;
    }

    @Override
    public Map<String, Object> getplayerdatamap(Map<String, Object> playerdatamap, PlayerData playerData) {
        playerdatamap.put("parameter", CollUtil.newArrayList(playerData.getGamesPlayed(),playerData.getTotalGoal(),playerData.getTotalAssist()
                ,playerData.getTotalShoot(),playerData.getTotalOntarget(),playerData.getTotalPass(),playerData.getTotalOkpass()
                ,playerData.getTotalSurpass(),playerData.getTotalOksurpass(),playerData.getTotalPreemption(),playerData.getTotalOffside(),
                playerData.getTotalFoul(),playerData.getTotalBefouled(),playerData.getTotalYcard(),playerData.getTotalRcard()));

        return playerdatamap;

    }

    @Override
    public Map<String, Object> getplayerattackdatamap(Map<String, Object> playerdatamap, PlayerData playerData) {
        playerdatamap.put("parameter", CollUtil.newArrayList(playerData.getGamesPlayed(),playerData.getTotalGoal(),playerData.getTotalAssist()
                ,playerData.getTotalShoot(),playerData.getTotalOntarget()
                ,playerData.getTotalSurpass(),playerData.getTotalOksurpass(),playerData.getTotalPreemption(),playerData.getTotalOffside(),
                playerData.getTotalFoul(),playerData.getTotalBefouled(),playerData.getTotalYcard(),playerData.getTotalRcard()));

        return playerdatamap;
    }

    @Override
    public Map<String, Object> getplayerpassdatamap(Map<String, Object> playerdatamap, PlayerData playerData) {
        playerdatamap.put("parameter", CollUtil.newArrayList(playerData.getTotalPass(),playerData.getTotalOkpass()));

        return playerdatamap;
    }

    @Override
    public Map<String, Object> getplayeravedatamap(Map<String, Object> playeravedatamap, PlayerData playerData) {

        playeravedatamap.put("aveparameter",CollUtil.newArrayList(playerData.getAverageGoal(),playerData.getAverageAssist(),playerData.getAverageShoot(),playerData.getAverageOntarget()
        ,playerData.getAveragePass(),playerData.getAverageOkpass(),playerData.getAverageSurpass(),playerData.getAverageOksurpass(),playerData.getAveragePreemption(),playerData.getAverageOffside(),
                playerData.getAverageFoul(),playerData.getAverageBefouled(),playerData.getAverageYcard(),playerData.getAverageRcard()));

        return playeravedatamap;
    }

    @Override
    public Map<String, Object> getplayeraveattackdatamap(Map<String, Object> playeravedatamap, PlayerData playerData) {
        playeravedatamap.put("aveparameter",CollUtil.newArrayList(playerData.getAverageGoal(),playerData.getAverageAssist(),playerData.getAverageShoot(),playerData.getAverageOntarget()
               ,playerData.getAverageSurpass(),playerData.getAverageOksurpass(),playerData.getAveragePreemption(),playerData.getAverageOffside(),
                playerData.getAverageFoul(),playerData.getAverageBefouled(),playerData.getAverageYcard(),playerData.getAverageRcard()));

        return playeravedatamap;
    }

    @Override
    public Map<String, Object> getplayeravepassdatamap(Map<String, Object> playeravedatamap, PlayerData playerData) {
        playeravedatamap.put("aveparameter",CollUtil.newArrayList(playerData.getAveragePass(),playerData.getAverageOkpass()));

        return playeravedatamap;
    }

    @Override
    public Map<String, Object> getpdsmap(Map<String, Object> pdsmap, PlayerDataSingle playerDataSingle) {

        pdsmap.put("parameter",CollUtil.newArrayList(playerDataSingle.getSingleGoal(),playerDataSingle.getSingleAssist(),playerDataSingle.getSingleShoot(),
                playerDataSingle.getSingleOntarget(),playerDataSingle.getSinglePass(),playerDataSingle.getSingleOkpass(),playerDataSingle.getSingleSurpass(),playerDataSingle.getSingleOksurpass(),
                playerDataSingle.getSinglePreemption(),playerDataSingle.getSingleOffside(),playerDataSingle.getSingleFoul(),playerDataSingle.getSingleBefouled(),playerDataSingle.getSingleYcard()
                ,playerDataSingle.getSingleRcard()));

        return pdsmap;
    }

    @Override
    public Map<String, Object> getattackpdsmap(Map<String, Object> pdsmap, PlayerDataSingle playerDataSingle) {
        pdsmap.put("parameter",CollUtil.newArrayList(playerDataSingle.getSingleGoal(),playerDataSingle.getSingleAssist(),playerDataSingle.getSingleShoot(),
                playerDataSingle.getSingleOntarget(),playerDataSingle.getSingleSurpass(),playerDataSingle.getSingleOksurpass()));

        return pdsmap;
    }

    @Override
    public Map<String, Object> getpasspdsmap(Map<String, Object> pdsmap, PlayerDataSingle playerDataSingle) {
        pdsmap.put("parameter",CollUtil.newArrayList(playerDataSingle.getSinglePass(),playerDataSingle.getSingleOkpass()));
        return pdsmap;
    }

    @Override
    public Map<String, Object> getfoulpdsmap(Map<String, Object> pdsmap, PlayerDataSingle playerDataSingle) {

        pdsmap.put("parameter",CollUtil.newArrayList(
                playerDataSingle.getSinglePreemption(),playerDataSingle.getSingleOffside(),playerDataSingle.getSingleFoul(),playerDataSingle.getSingleBefouled(),playerDataSingle.getSingleYcard()
                ,playerDataSingle.getSingleRcard()));

        return pdsmap;
    }


}

